package com.homedo.odin.solution.controller.pc;

import com.homedo.core.common.base.controller.BaseController;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.core.common.exception.BizException;
import com.homedo.odin.solution.contract.pc.ExportExcelReq;
import com.homedo.odin.solution.contract.pc.ExportExcelResp;
import com.homedo.odin.solution.contract.pc.GetSolProductDetailListResp;
import com.homedo.odin.solution.contract.pc.RelatedSolutionResponseBean;
import com.homedo.odin.solution.contract.pc.RelatedSolutionResponseBeanResp;
import com.homedo.odin.solution.contract.pc.SolutionSkuImportReq;
import com.homedo.odin.solution.model.entity.Solution;
import com.homedo.odin.solution.service.SolutionServiceImpl;
import com.homedo.odin.solution.service.SolutionSkuServiceImpl;
import com.homedo.odin.solution.service.bizservice.pc.SolutionsBizService;
import com.homedo.odin.solution.service.bizservice.personal.CommonBizService;
import com.homedo.odin.solution.service.bizservice.personal.ExportProductListBizService;
import com.homedo.odin.solution.service.bizservice.personal.SolProductDetailBizService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 注：后期和 SolutionsController 合并
 *
 * @author: donglibing
 * @date: 2018/3/6 14:39
 * @Description:
 */
@Api(description = "方案", tags = {"Solution"}, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/solution")
public class SolutionController extends BaseController<ExportProductListBizService> {

    @Autowired
    SolProductDetailBizService solProductDetailBizService;

    @Autowired
    CommonBizService commonBizService;

    @Autowired
    SolutionSkuServiceImpl solutionSkuService;

    @Autowired
    SolutionsBizService solutionsBizService;

    @Autowired
    SolutionServiceImpl solutionsService;

    @ApiOperation(value = "清单列表", notes = "清单列表", httpMethod = "GET", response = GetSolProductDetailListResp.class)
    @GetMapping("/solutionlisting/{id}")
    public ResultData solutionListing(@ApiParam(value = "方案id", required = true) @PathVariable("id") String id) {
        GetSolProductDetailListResp Object = solProductDetailBizService.getSolProductDetailList(id);
        return ResultData.newInstance(Meta.CODE_200, Object);
    }

    @ApiOperation(value = "插入配置后的清单", notes = "插入配置后的清单", httpMethod = "POST", response = ExportExcelResp.class)
    @PostMapping(value = "/inventory")
    public ResultData insertInventory(@RequestBody ExportExcelReq req) throws Exception {
        ExportExcelResp resp = baseService.exportSolution(req);
        return ResultData.newInstance(Meta.CODE_200, resp);
    }

    @ApiOperation(value = "下载清单", notes = "下载清单", httpMethod = "GET")
    @GetMapping(value = "/exportexcel/{id}")
    public void downSolutionListing(@ApiParam(value = "配置后的清单Id", required = true) @PathVariable("id") int id,
            HttpServletResponse response) throws Exception {
        baseService.export(id, response);
    }

    @ApiOperation(value = "PC端_新增方案关联SKU", notes = "PC端_新增方案关联SKU", httpMethod = "POST")
    @PostMapping(value = "/skus")
    public ResultData addSkus(@RequestBody SolutionSkuImportReq solutionSkuImportReq) throws Exception {
        solutionSkuService.addSkus(solutionSkuImportReq);
        return ResultData.newInstance(Meta.CODE_200, "关联方案成功");
    }



    @ApiOperation(value = "获取相关系统(专家解决)方案", response = RelatedSolutionResponseBeanResp.class, httpMethod = "GET")
    @GetMapping("/related")
    public ResultData queryRelatedSolution(
        @ApiParam(value = "方案id", required = true) @RequestParam("solutionId") Long solutionId,
        @ApiParam(value = "条数(最多取5条)", required = true) @RequestParam("num") Integer num) {
        Solution solution = solutionsService.getSolution(solutionId);
        if (solution == null) {
            throw new BizException(Meta.CODE_451.getCode(),"该方案不存在");
        }
        List<RelatedSolutionResponseBean> relatedSolutionResponseBeans = baseService.queryRelatedSolution(solution,solutionId,num);
        RelatedSolutionResponseBeanResp resp = new RelatedSolutionResponseBeanResp();
        resp.setList(relatedSolutionResponseBeans);
        return ResultData.newInstance(resp);
    }


}
